//
//  GetInvoiceViewController.m
//  YJRenter
//
//  Created by 文彬 on 2018/7/8.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "GetInvoiceViewController.h"
#import "InputItemTableViewCell.h"
#import "InvoiceTypeTableViewCell.h"
#import "RadioSelectTableViewCell.h"
#import "WBTextView.h"

typedef enum {
    kInvoiceTypePersonPaper,
    kInvoiceTypePersonElectronic,
    kInvoiceTypeCompanyPaper,
    kInvoiceTypeCompanyElectronic
}kInvoiceType;

@interface GetInvoiceViewController ()<UITableViewDelegate,
    UITableViewDataSource>
{
    kInvoiceType invoiceType;
}

@property (strong, nonatomic) UITableView *listTableView;


@end

@implementation GetInvoiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"开具发票";
    
    invoiceType = kInvoiceTypePersonElectronic;
    
    [self initPageControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark 页面相关
- (void)initPageControl{
    
    [StaticTools addNavgationRightButtonWithController:self title:@"开票历史" font:[UIFont systemFontOfSize:15] color:kSecTitleColor click:^(id param) {
    }];
    
    [StaticTools addPageFootButton:self.view title:@"提交" titleFont:[UIFont systemFontOfSize:18] backgroundColor:kMainOriginColor titleColor:[UIColor whiteColor] buttonHeight:40 bottomMargin:5 leftMargin:10 cornerRadius:5 click:^(id param) {
        
    }];
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.bottom.equalTo(@-50);
    }];
    
    [self addTableFootView];
}

- (void)addTableFootView{
    WBTextView *txtView = [[WBTextView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    txtView.font = [UIFont systemFontOfSize:15];
    txtView.placeholder = @"请填写发票备注内容(100字以内)";
    self.listTableView.tableFooterView = txtView;
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==2){
        if (invoiceType == kInvoiceTypePersonPaper||
            invoiceType == kInvoiceTypePersonElectronic) {
            return 3+1;
        }
        return 4+1;
    }
    else if(section==3){
        if (invoiceType == kInvoiceTypePersonPaper||
            invoiceType == kInvoiceTypeCompanyPaper) {
            return 5+1; //空白间隙加一行
        }
        else{
            return 2+1;
        }
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 60;
    }
    else if(indexPath.section==1){
        return 50;
    }
    else if((indexPath.section==3&&indexPath.row==0)||
            (indexPath.section==2&&indexPath.row==0)){
        return 10; //空白间隔行
    }
    
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        InvoiceTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InvoiceTypeTableViewCell" forIndexPath:indexPath];
        return cell;
    }
    else if (indexPath.section==1) {
        
        InputItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InputItemTableViewCell" forIndexPath:indexPath];
        cell.titleLabl.text = @"发票内容";
        cell.inputTxtField.text = @"住宿服务费";
        return cell;
    }
    else if(indexPath.section==2){
        if (indexPath.row==0||indexPath.row==1||indexPath.row==2) {
            RadioSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RadioSelectTableViewCell" forIndexPath:indexPath];
            cell.radioView.hidden = NO;
            if(indexPath.row==0){
                cell.titleLabel.text = @"";
                cell.radioView.hidden = YES;
            }
            else if (indexPath.row==1) {
                cell.titleLabel.text = @"发票类型";
                [cell.radioView setLeftTitle:@"个人" rightTitle:@"公司"];
                [cell.radioView remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(cell.contentView.centerY);
                    make.height.equalTo(@30);
                    make.right.equalTo(@-10);
                    make.width.equalTo(@140);
                }];
                cell.radioView.valueChanged = ^(id param) {
                    if ([param integerValue]==0) {
                        if (invoiceType == kInvoiceTypeCompanyElectronic) {
                            invoiceType = kInvoiceTypePersonElectronic;
                        }
                        else{
                            invoiceType = kInvoiceTypePersonPaper;
                        }
                    }
                    else{
                        if (invoiceType==kInvoiceTypePersonPaper) {
                            invoiceType = kInvoiceTypeCompanyPaper;
                        }
                        else{
                            invoiceType = kInvoiceTypeCompanyElectronic;
                        }
                    }
                    [tableView reloadData];
                };
                
            }
            else{
                cell.titleLabel.text = @"发票选项";
                [cell.radioView setLeftTitle:@"电子发票" rightTitle:@"纸质发票"];
                [cell.radioView remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(cell.contentView.centerY);
                    make.height.equalTo(@30);
                    make.right.equalTo(@-10);
                    make.width.equalTo(@200);
                }];
                cell.radioView.valueChanged = ^(id param) {
                    if ([param integerValue]==0) {
                        if (invoiceType == kInvoiceTypePersonPaper) {
                            invoiceType = kInvoiceTypePersonElectronic;
                        }
                        else{
                            invoiceType = kInvoiceTypeCompanyElectronic;
                        }
                    }
                    else{
                        if (invoiceType==kInvoiceTypePersonElectronic) {
                            invoiceType = kInvoiceTypePersonPaper;
                        }
                        else{
                            invoiceType = kInvoiceTypeCompanyPaper;
                        }
                    }
                    [tableView reloadData];
                };
            }
            return cell;
        }
        else if (indexPath.row==3||indexPath.row==4) {
            InputItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InputItemTableViewCell" forIndexPath:indexPath];
            cell.inputTxtField.text = @"";
            if (indexPath.row==3) {
                cell.titleLabl.text = @"发票抬头";
                if (invoiceType == kInvoiceTypePersonElectronic||
                    invoiceType == kInvoiceTypePersonPaper) {
                    cell.inputTxtField.text = @"个人";
                    cell.inputTxtField.enabled = NO;
                }
                else{
                    cell.inputTxtField.enabled = YES;
                    cell.inputTxtField.placeholder = @"请输入发票抬头";
                }
               
            }
            else{
                cell.inputTxtField.enabled = YES;
                cell.titleLabl.text = @"纳税人识别号";
                cell.inputTxtField.placeholder = @"请输入纳税人识别号";
            }
          
            return cell;
        }
    }
    else if(indexPath.section==3){
        InputItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InputItemTableViewCell" forIndexPath:indexPath];
        cell.inputTxtField.enabled = YES;
        cell.inputTxtField.text = @"";
        cell.inputTxtField.placeholder = @"";
        cell.titleLabl.text = @"";
        if(indexPath.row==0){
            cell.inputTxtField.enabled = NO;
        }
        
        if (invoiceType == kInvoiceTypePersonPaper||
            invoiceType == kInvoiceTypeCompanyPaper) {
            if (indexPath.row==1) {
                cell.titleLabl.text = @"收件人";
                cell.inputTxtField.placeholder = @"请输入收件人姓名";
            }
            else if (indexPath.row==2) {
                cell.titleLabl.text = @"联系方式";
                cell.inputTxtField.placeholder = @"请输入联系方式";
            }
            else if (indexPath.row==3) {
                cell.titleLabl.text = @"邮政编码";
                cell.inputTxtField.placeholder = @"请输入邮政编码";
            }
            else if (indexPath.row==4) {
                cell.titleLabl.text = @"详细地址";
                cell.inputTxtField.placeholder = @"请输入详细地址";
            }
            else if (indexPath.row==5) {
                cell.titleLabl.text = @"备注(选填)";
                cell.inputTxtField.text = @"编辑";
                cell.inputTxtField.enabled = NO;
            }
        }
        else{
            if (indexPath.row==1) {
                cell.titleLabl.text = @"电子邮箱";
                cell.inputTxtField.placeholder = @"请输入电子邮箱";
            }
            else if (indexPath.row==2) {
                cell.titleLabl.text = @"备注(选填)";
                cell.inputTxtField.text = @"编辑";
            }
        }
        return cell;
    }
    NSString *iden = @"iden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    
    return cell;
}

#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = [UIColor clearColor];
        [_listTableView registerNib:[UINib nibWithNibName:@"InputItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"InputItemTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"InvoiceTypeTableViewCell" bundle:nil] forCellReuseIdentifier:@"InvoiceTypeTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"RadioSelectTableViewCell" bundle:nil] forCellReuseIdentifier:@"RadioSelectTableViewCell"];
   
        
    }
    
    return _listTableView;
}


@end
